package projectManagementServer;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

public class AssignLeaderGUI extends JPanel implements ActionListener{
	
	DefaultListModel<String> membersListModel = new DefaultListModel<>();
	JList<String> membersList = new JList<>();
	
	ArrayList<Member> allMember;
	String project;
	
	JFrame f;
	private JTextField currentLeader;
	private String currLeader;
	
	public AssignLeaderGUI(String project, ArrayList<Member> allMember, String currLeader) {
		setPreferredSize(new Dimension(301, 468));
		setLayout(null);
		
		this.project = project;
		this.allMember = allMember;
		
		membersPanel();
		
		f = new JFrame();
		f.getContentPane().add(this);
		
		currentLeader = new JTextField();
		currentLeader.setText(currLeader);
		currentLeader.setEditable(false);
		currentLeader.setBounds(10, 391, 281, 33);
		add(currentLeader);
		currentLeader.setColumns(10);
		
		JLabel lblCurrentProjectLeader = new JLabel("Current Project Leader:");
		lblCurrentProjectLeader.setBounds(10, 378, 156, 14);
		add(lblCurrentProjectLeader);
		f.setVisible(true);
		f.setResizable(false);
		f.setDefaultCloseOperation(WindowConstants.HIDE_ON_CLOSE);
		f.pack();
		f.setLocationRelativeTo(null);
		
		
	}
	
	public void membersPanel(){
		JPanel membersPanel = new JPanel();
		membersPanel.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "All Members", TitledBorder.CENTER, TitledBorder.TOP, null, null));
		membersPanel.setBounds(10, 11, 281, 363);
		membersPanel.setLayout(new BorderLayout(0, 0));
		add(membersPanel);
		
		
		membersListModel = new DefaultListModel<>();		
		membersList = new JList<>();
		
		for(Member x : allMember){
			membersListModel.addElement(x.getUserName());
		}

		membersList.setFont(new Font("Tempus Sans ITC", Font.PLAIN, 15));
		membersList.setModel(membersListModel);
		membersList.setSelectedIndex(-1);
		membersList.setVisibleRowCount(0);
		membersList.setValueIsAdjusting(true);
		membersList.setLocation(0,0);
		membersList.getSelectionModel().setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		membersList.putClientProperty( "Quaqua.List.style", "striped");
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportView(membersList);
		scrollPane.setBorder(new EmptyBorder(0,0,0,0));
		membersPanel.add(scrollPane, BorderLayout.CENTER);
		
		JButton btnAssign = new JButton("Assign");
		btnAssign.setBounds(60, 424, 89, 33);
		btnAssign.addActionListener(this);
		btnAssign.putClientProperty("Quaqua.Button.style", "bevel");
		add(btnAssign);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.setBounds(145, 424, 89, 33);
		btnCancel.addActionListener(this);
		btnCancel.putClientProperty("Quaqua.Button.style", "bevel");
		add(btnCancel);
	}

	@Override
	public void actionPerformed(ActionEvent btn) {
		String cmd = btn.getActionCommand();
		
		if(cmd.equalsIgnoreCase("Assign")){
			try{
				AccessDatabase db = new AccessDatabase();
				db.assignProjectLeader(project, membersListModel.getElementAt(membersList.getSelectedIndex()));
				ServerGUI.addProjectLeader(project, membersListModel.getElementAt(membersList.getSelectedIndex()));
			}catch(Exception e){
				e.printStackTrace();
			}
			f.setVisible(false);	
		}
		
		if(cmd.equalsIgnoreCase("Cancel")){
			f.setVisible(false);
		}
		
	}
}
